package com.wondersgroup.insurance.dnuilearn.views.xformode;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.wondersgroup.insurance.dnuilearn.R;

/**
 * Created by Administrator on 2017/5/15.
 */

public class HeartMapView extends View {
    Paint mPaint;

    Bitmap bmp = null;

    int maxWidth = 0;
    private Bitmap mBitMapSRC;


    int dx;
    public HeartMapView(Context context) {
        this(context, null);
    }

    public HeartMapView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HeartMapView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(View.LAYER_TYPE_SOFTWARE,null);

        mPaint=new Paint();
        mPaint.setColor(Color.RED);
        bmp = BitmapFactory.decodeResource(getResources(), R.drawable.heartmap);

        maxWidth = getWidth();
        mBitMapSRC = Bitmap.createBitmap(bmp.getWidth(),bmp.getHeight(), Bitmap.Config.ARGB_8888);


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        dx += 5;
     /*   mPaint.setColor(Color.WHITE);
        maxWidth -= 5;
        if (maxWidth <=0){
            maxWidth = getWidth();
        }
        canvas.drawRect(0,0,maxWidth,bmp.getHeight(),mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
        mPaint.setColor(Color.RED);
        canvas.drawBitmap(bmp,0,0,mPaint);



        mPaint.setXfermode(null);*/




        // 清空src bitmap的内容，变成透明的
        Canvas c = new Canvas(mBitMapSRC);
        c.drawColor(Color.RED, PorterDuff.Mode.CLEAR);

        // 画不透明的矩形区域
        c.drawRect(bmp.getWidth() - dx, 0 , bmp.getWidth(),bmp.getHeight(),mPaint);
        canvas.drawBitmap(mBitMapSRC,0,0,mPaint);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(bmp,0,0,mPaint);
        mPaint.setXfermode(null);
        postInvalidateDelayed(1000);
        dx += 5;
    }
}
